//package cn.com.headfree.listener;
//
//import cn.com.headfree.filter.RedisSessionFilter;
//import cn.com.headfree.server.dao.IServerRecordDao;
//import cn.com.headfree.server.entity.TZuulServerRecord;
//import com.alibaba.fastjson.JSONObject;
//import org.apache.log4j.Logger;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.context.ApplicationListener;
//import org.springframework.context.event.ContextRefreshedEvent;
//import org.springframework.stereotype.Component;
//
//import java.io.BufferedReader;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.InputStreamReader;
//import java.text.ParseException;
//import java.text.SimpleDateFormat;
//import java.util.*;
//
///**
// * @description:定时读取路由访问信息入表
// * @Author: panl
// * @Date: 2020/3/2 15:29
// */
//@Component
//public class ZuulRecordListener implements ApplicationListener<ContextRefreshedEvent> {
//    private static final Logger logger = Logger.getLogger(ZuulRecordListener.class);
//
//    private String fileName;
//    @Value("${zuulRecord.path}${spring.application.name}${server.port}")
//    private String basePath;
//
//    @Autowired
//    private IServerRecordDao iServerRecordDao;
//
//    @Override
//    public void onApplicationEvent(ContextRefreshedEvent event) {
//
//        //创建定时器
//        Timer timer = new Timer();
//        Calendar calendar = Calendar.getInstance();
//        calendar.add(Calendar.DATE, 1);
//        calendar.set(calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DATE), 0, 0, 0);
//        long timeInterval = 24 * 60 * 30 * 1000;//间隔24小时
//        try {
//            timer.schedule(new TimerTask() {//每天00:00进行访问记录入表
//                @Override
//                public void run() {
//                    // 每天00:00需要做的事情
//                    logger.info("开始执行访问路由记录入表定时器！");
////                    readForTxt();
//                    RedisSessionFilter re = new RedisSessionFilter();
//                    List<TZuulServerRecord> list = re.recordList;
//                }
//              }, calendar.getTime(), timeInterval);
////            }, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2020-03-03 10:45:00"));
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }
//
//    /*
//     * 功能描述: 读取文件中存储的访问信息
//     * @Author: panl
//     * @Date: 2020/3/2 13:46
//     */
//    public void readForTxt() {
//        File file = new File(basePath);
//        File[] tempList = file.listFiles();
////        String fileName = "";
//        for (int i = 0; i < tempList.length; i++) {
//            List<TZuulServerRecord> list = new ArrayList<TZuulServerRecord>();
//            if (tempList[i].isFile()) {
//                logger.info("文件名称：" + tempList[i].getName());
//                fileName = basePath + "\\" + tempList[i].getName();
//                if (fileName != "" && !fileName.contains("output")) {
//                    /* 读取数据 */
//                    try {
//                        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName)),
//                                "UTF-8"));
//                        String lineTxt = null;
//                        logger.info("开始读取访问路由记录文件内容！");
//                        while ((lineTxt = br.readLine()) != null) {
//                            list.add(JSONObject.parseObject(lineTxt, TZuulServerRecord.class));
//                            //System.err.println("读取的内容:" + list.size() + "," + JSON.toJSONString(list.get(list.size() - 1)));
//                        }
//                        br.close();
//                        logger.info("读取访问路由记录文件内容完成共计："+list.size()+"条记录！");
//                        if(list!=null&&list.size()>0){
//                            int num = iServerRecordDao.saveZuulRecord(list);
//                            if(num>0){
//                                File file2 = new File(fileName);
//                                if(file2.exists()){
//                                    logger.info("开始删除访问路由记录文件，文件名称："+file2.getName());
//                                    file2.delete();
//                                }
//                            }
//                        }
//                    } catch (Exception e) {
//                        System.err.println("read errors :" + e);
//                    }
//                } else {
//                    fileName = "";
//                    continue;
//                }
//            }
//        }
//        //return list;
//    }
//}
